package com.example.myrecipes;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

public class EditRecipe extends Activity {
	Recipe recipe;
	ArrayAdapter<String> ingredientsAdapter;
	ArrayAdapter<String> instructionsAdapter;
	
	Calendar dateTime = Calendar.getInstance();
	private TextView prepTimeText;
	private TextView cookTimeText;
	private TextView readyInTimeText;
	private int prepTimeHour = 0;
	private int prepTimeMinute = 0;
	private int cookTimeHour = 0;
	private int cookTimeMinute = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setupCustomOptionMenu();
				
		setContentView(R.layout.activity_edit_recipe);
		
		prepTimeText = (TextView) findViewById(R.id.prepTime);
		cookTimeText = (TextView) findViewById(R.id.cookTime);
		readyInTimeText = (TextView) findViewById(R.id.readyInTime);

	} 

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private void setupCustomOptionMenu() {
		LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
		final View customActionBarView = inflater.inflate(R.layout.edit_recipe_menu, null);

		recipe = new Recipe();

		customActionBarView.findViewById(R.id.actionbar_save).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						saveRecipe(view);
					}
				});

		customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		setupActionBar(customActionBarView);
	}

	private void setupActionBar(View customActionBarView) {
		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(
				ActionBar.DISPLAY_SHOW_CUSTOM,
				ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
				| ActionBar.DISPLAY_SHOW_TITLE);
		actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
	}
	public void saveRecipe(View view) {
		EditText recipeNameText = (EditText) findViewById(R.id.edit_recipe_name);
		EditText yieldText = (EditText) findViewById(R.id.edit_yield);
		
		String recipeName = recipeNameText.getText().toString();
		String prepTime = prepTimeText.getText().toString();
		String cookTime = cookTimeText.getText().toString();
		String readyInTime = readyInTimeText.getText().toString();
		String yield = yieldText.getText().toString();
		
		if(recipeName.trim().equals("")) {
			AlertDialog.Builder createRecipeAlert = new AlertDialog.Builder(this);          
			createRecipeAlert.setTitle("Empty recipe name.")
			.setCancelable(true)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					//Do nothing...
				}
			});

			AlertDialog alert = createRecipeAlert.create();
			alert.show();
		}
		else {			
			recipe.setRecipeName(recipeName);
			recipe.setPrepTime(prepTime);
			recipe.setCookTime(cookTime);
			recipe.setReadyInTime(readyInTime);
			recipe.setYield(yield);
			Intent recipeIntent = new Intent();
			recipeIntent.putExtra("Recipe", recipe);
			setResult(Activity.RESULT_OK, recipeIntent);
			finish();
		}
	}

	public void addIngredient(View view) {
		EditText ingredientText = (EditText) findViewById(R.id.edit_ingredient);
		String ingredient = ingredientText.getText().toString();

		recipe.addIngredient(ingredient);
		HashSet<String> ingredientList = recipe.getIngredients();
		ArrayList<String> ingredientsArr = new ArrayList<String>(ingredientList);

		ListView ingredientsView = (ListView) findViewById(R.id.list_ingredients);
		ingredientsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ingredientsArr);
		ingredientsView.setAdapter(ingredientsAdapter);

		ingredientText.setText("");
	}

	public void addInstruction(View view) {
		EditText instructionText = (EditText) findViewById(R.id.edit_instruction);
		String instruction = instructionText.getText().toString();

		recipe.addInstructions(instruction);
		ArrayList<String> instructionList = recipe.getInstructions();

		ListView instructionView = (ListView) findViewById(R.id.list_instructions);
		instructionsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, instructionList);
		instructionView.setAdapter(instructionsAdapter);

		instructionText.setText("");
	}
	
	public void setPrepTime(View view) {
    	new TimePickerDialog(EditRecipe.this, prep, 0, 0, true).show();
	}
	
	TimePickerDialog.OnTimeSetListener prep = new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int prepHour, int prepMinute) {
			prepTimeHour = prepHour;
			prepTimeMinute = prepMinute;
			String readyInTime = updateReadyInTime();
			updateTime(getTime(prepHour, prepMinute), prepTimeText);
			updateTime(readyInTime, readyInTimeText);
		}
	};
	
	private String updateReadyInTime() {
		return getTime(prepTimeHour + cookTimeHour, prepTimeMinute + cookTimeMinute);
	}
	
	private void updateTime(String time, TextView textView) {
		textView.setText(time);
	}
	
	private String getTime(int hour, int minute) {
		StringBuffer time = new StringBuffer();
		if(minute > 60) {
			hour = hour + minute/60;
			minute = minute % 60;
		}
		time.append(String.format("%d mins", minute));
		if(hour > 0) {
			time.insert(0, String.format("%d hours ", hour, minute));
		}
		return time.toString();
	}
	
	public void setCookTime(View view) {
    	new TimePickerDialog(EditRecipe.this, cook, 0, 0, true).show();		
	}
	
	TimePickerDialog.OnTimeSetListener cook = new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int cookHour, int cookMinute) {
			cookTimeHour = cookHour;
			cookTimeMinute = cookMinute;
			String readyInTime = updateReadyInTime();
			updateTime(getTime(cookHour, cookMinute), cookTimeText);
			updateTime(readyInTime, readyInTimeText);
		}
	};
	
}
